17a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos/* 27a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos * Copyright (C) 2014 The Android Open Source Project 37a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos * 47a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos * Licensed under the Apache License, Version 2.0 (the "License"); 57a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos * you may not use this file except in compliance with the License. 67a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos * You may obtain a copy of the License at 77a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos * 87a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos * http://www.apache.org/licenses/LICENSE-2.0 97a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos * 107a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos * Unless required by applicable law or agreed to in writing, software 117a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos * distributed under the License is distributed on an "AS IS" BASIS, 127a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 137a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos * See the License for the specific language governing permissions and 147a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos * limitations under the License 157a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos */ 167a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos 177a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roospackage com.android.server.trust; 187a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos 197a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roosimport android.content.ComponentName; 207a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roosimport android.os.SystemClock; 217a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roosimport android.os.UserHandle; 2294e15a59b757678949cccb5d783bee1638e84697Adrian Roosimport android.service.trust.TrustAgentService; 237a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roosimport android.util.TimeUtils; 247a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos 257a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roosimport java.io.PrintWriter; 267a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roosimport java.util.ArrayDeque; 277a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roosimport java.util.Iterator; 287a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos 297a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos/** 307a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos * An archive of trust events. 317a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos */ 327a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roospublic class TrustArchive { 337a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos private static final int TYPE_GRANT_TRUST = 0; 347a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos private static final int TYPE_REVOKE_TRUST = 1; 357a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos private static final int TYPE_TRUST_TIMEOUT = 2; 367a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos private static final int TYPE_AGENT_DIED = 3; 377d59b4f981e24a4a446522e9b8d3d6a7115c1459Adrian Roos private static final int TYPE_AGENT_CONNECTED = 4; 387d59b4f981e24a4a446522e9b8d3d6a7115c1459Adrian Roos private static final int TYPE_AGENT_STOPPED = 5; 397861c663fd64af33ec2a4c5ad653c806dc8bd994Adrian Roos private static final int TYPE_MANAGING_TRUST = 6; 407a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos 417a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos private static final int HISTORY_LIMIT = 200; 427a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos 437a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos private static class Event { 447a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos final int type; 457a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos final int userId; 467a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos final ComponentName agent; 477a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos final long elapsedTimestamp; 487a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos 497a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos // grantTrust 507a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos final String message; 517a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos final long duration; 5294e15a59b757678949cccb5d783bee1638e84697Adrian Roos final int flags; 537a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos 547861c663fd64af33ec2a4c5ad653c806dc8bd994Adrian Roos // managingTrust 557861c663fd64af33ec2a4c5ad653c806dc8bd994Adrian Roos final boolean managingTrust; 567861c663fd64af33ec2a4c5ad653c806dc8bd994Adrian Roos 577a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos private Event(int type, int userId, ComponentName agent, String message, 5894e15a59b757678949cccb5d783bee1638e84697Adrian Roos long duration, int flags, boolean managingTrust) { 597a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos this.type = type; 607a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos this.userId = userId; 617a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos this.agent = agent; 627a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos this.elapsedTimestamp = SystemClock.elapsedRealtime(); 637a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos this.message = message; 647a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos this.duration = duration; 6594e15a59b757678949cccb5d783bee1638e84697Adrian Roos this.flags = flags; 667861c663fd64af33ec2a4c5ad653c806dc8bd994Adrian Roos this.managingTrust = managingTrust; 677a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos } 687a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos } 697a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos 707a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos ArrayDeque<Event> mEvents = new ArrayDeque<Event>(); 717a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos 727a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos public void logGrantTrust(int userId, ComponentName agent, String message, 7394e15a59b757678949cccb5d783bee1638e84697Adrian Roos long duration, int flags) { 747a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos addEvent(new Event(TYPE_GRANT_TRUST, userId, agent, message, duration, 7594e15a59b757678949cccb5d783bee1638e84697Adrian Roos flags, false)); 767a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos } 777a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos 787a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos public void logRevokeTrust(int userId, ComponentName agent) { 7994e15a59b757678949cccb5d783bee1638e84697Adrian Roos addEvent(new Event(TYPE_REVOKE_TRUST, userId, agent, null, 0, 0, false)); 807a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos } 817a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos 827a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos public void logTrustTimeout(int userId, ComponentName agent) { 8394e15a59b757678949cccb5d783bee1638e84697Adrian Roos addEvent(new Event(TYPE_TRUST_TIMEOUT, userId, agent, null, 0, 0, false)); 847a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos } 857a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos 867a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos public void logAgentDied(int userId, ComponentName agent) { 8794e15a59b757678949cccb5d783bee1638e84697Adrian Roos addEvent(new Event(TYPE_AGENT_DIED, userId, agent, null, 0, 0, false)); 887a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos } 897a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos 907d59b4f981e24a4a446522e9b8d3d6a7115c1459Adrian Roos public void logAgentConnected(int userId, ComponentName agent) { 9194e15a59b757678949cccb5d783bee1638e84697Adrian Roos addEvent(new Event(TYPE_AGENT_CONNECTED, userId, agent, null, 0, 0, false)); 927d59b4f981e24a4a446522e9b8d3d6a7115c1459Adrian Roos } 937d59b4f981e24a4a446522e9b8d3d6a7115c1459Adrian Roos 947d59b4f981e24a4a446522e9b8d3d6a7115c1459Adrian Roos public void logAgentStopped(int userId, ComponentName agent) { 9594e15a59b757678949cccb5d783bee1638e84697Adrian Roos addEvent(new Event(TYPE_AGENT_STOPPED, userId, agent, null, 0, 0, false)); 967861c663fd64af33ec2a4c5ad653c806dc8bd994Adrian Roos } 977861c663fd64af33ec2a4c5ad653c806dc8bd994Adrian Roos 987861c663fd64af33ec2a4c5ad653c806dc8bd994Adrian Roos public void logManagingTrust(int userId, ComponentName agent, boolean managing) { 9994e15a59b757678949cccb5d783bee1638e84697Adrian Roos addEvent(new Event(TYPE_MANAGING_TRUST, userId, agent, null, 0, 0, managing)); 1007d59b4f981e24a4a446522e9b8d3d6a7115c1459Adrian Roos } 1017d59b4f981e24a4a446522e9b8d3d6a7115c1459Adrian Roos 1027a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos private void addEvent(Event e) { 1037a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos if (mEvents.size() >= HISTORY_LIMIT) { 1047a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos mEvents.removeFirst(); 1057a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos } 1067a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos mEvents.addLast(e); 1077a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos } 1087a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos 1097a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos public void dump(PrintWriter writer, int limit, int userId, String linePrefix, 1107a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos boolean duplicateSimpleNames) { 1117a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos int count = 0; 1127a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos Iterator<Event> iter = mEvents.descendingIterator(); 1137a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos while (iter.hasNext() && count < limit) { 1147a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos Event ev = iter.next(); 1157a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos if (userId != UserHandle.USER_ALL && userId != ev.userId) { 1167a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos continue; 1177a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos } 1187a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos 1197a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos writer.print(linePrefix); 1207a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos writer.printf("#%-2d %s %s: ", count, formatElapsed(ev.elapsedTimestamp), 1217a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos dumpType(ev.type)); 1227a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos if (userId == UserHandle.USER_ALL) { 1237a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos writer.print("user="); writer.print(ev.userId); writer.print(", "); 1247a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos } 1257a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos writer.print("agent="); 1267a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos if (duplicateSimpleNames) { 1277a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos writer.print(ev.agent.flattenToShortString()); 1287a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos } else { 1297a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos writer.print(getSimpleName(ev.agent)); 1307a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos } 1317a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos switch (ev.type) { 1327a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos case TYPE_GRANT_TRUST: 13394e15a59b757678949cccb5d783bee1638e84697Adrian Roos writer.printf(", message=\"%s\", duration=%s, flags=%s", 13494e15a59b757678949cccb5d783bee1638e84697Adrian Roos ev.message, formatDuration(ev.duration), dumpGrantFlags(ev.flags)); 1357a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos break; 1367861c663fd64af33ec2a4c5ad653c806dc8bd994Adrian Roos case TYPE_MANAGING_TRUST: 1377861c663fd64af33ec2a4c5ad653c806dc8bd994Adrian Roos writer.printf(", managingTrust=" + ev.managingTrust); 1387861c663fd64af33ec2a4c5ad653c806dc8bd994Adrian Roos break; 1397a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos default: 1407a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos } 1417a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos writer.println(); 1427a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos count++; 1437a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos } 1447a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos } 1457a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos 146c5f95cea2639b698594a85acbde6a5519941d7b1Adrian Roos public static String formatDuration(long duration) { 1477a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos StringBuilder sb = new StringBuilder(); 1487a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos TimeUtils.formatDuration(duration, sb); 1497a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos return sb.toString(); 1507a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos } 1517a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos 1527a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos private static String formatElapsed(long elapsed) { 1537a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos long delta = elapsed - SystemClock.elapsedRealtime(); 1547a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos long wallTime = delta + System.currentTimeMillis(); 1557a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos return TimeUtils.logTimeOfDay(wallTime); 1567a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos } 1577a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos 1587a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos /* package */ static String getSimpleName(ComponentName cn) { 1597a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos String name = cn.getClassName(); 1607a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos int idx = name.lastIndexOf('.'); 1617a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos if (idx < name.length() && idx >= 0) { 1627a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos return name.substring(idx + 1); 1637a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos } else { 1647a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos return name; 1657a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos } 1667a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos } 1677a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos 1687a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos private String dumpType(int type) { 1697a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos switch (type) { 1707a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos case TYPE_GRANT_TRUST: 1717a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos return "GrantTrust"; 1727a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos case TYPE_REVOKE_TRUST: 1737a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos return "RevokeTrust"; 1747a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos case TYPE_TRUST_TIMEOUT: 1757a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos return "TrustTimeout"; 1767a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos case TYPE_AGENT_DIED: 1777a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos return "AgentDied"; 1787d59b4f981e24a4a446522e9b8d3d6a7115c1459Adrian Roos case TYPE_AGENT_CONNECTED: 1797d59b4f981e24a4a446522e9b8d3d6a7115c1459Adrian Roos return "AgentConnected"; 1807d59b4f981e24a4a446522e9b8d3d6a7115c1459Adrian Roos case TYPE_AGENT_STOPPED: 1817d59b4f981e24a4a446522e9b8d3d6a7115c1459Adrian Roos return "AgentStopped"; 1827861c663fd64af33ec2a4c5ad653c806dc8bd994Adrian Roos case TYPE_MANAGING_TRUST: 1837861c663fd64af33ec2a4c5ad653c806dc8bd994Adrian Roos return "ManagingTrust"; 1847a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos default: 1857a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos return "Unknown(" + type + ")"; 1867a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos } 1877a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos } 18894e15a59b757678949cccb5d783bee1638e84697Adrian Roos 18994e15a59b757678949cccb5d783bee1638e84697Adrian Roos private String dumpGrantFlags(int flags) { 19094e15a59b757678949cccb5d783bee1638e84697Adrian Roos StringBuilder sb = new StringBuilder(); 19194e15a59b757678949cccb5d783bee1638e84697Adrian Roos if ((flags & TrustAgentService.FLAG_GRANT_TRUST_INITIATED_BY_USER) != 0) { 19294e15a59b757678949cccb5d783bee1638e84697Adrian Roos if (sb.length() != 0) sb.append('|'); 19394e15a59b757678949cccb5d783bee1638e84697Adrian Roos sb.append("INITIATED_BY_USER"); 19494e15a59b757678949cccb5d783bee1638e84697Adrian Roos } 19594e15a59b757678949cccb5d783bee1638e84697Adrian Roos if ((flags & TrustAgentService.FLAG_GRANT_TRUST_DISMISS_KEYGUARD) != 0) { 19694e15a59b757678949cccb5d783bee1638e84697Adrian Roos if (sb.length() != 0) sb.append('|'); 19794e15a59b757678949cccb5d783bee1638e84697Adrian Roos sb.append("DISMISS_KEYGUARD"); 19894e15a59b757678949cccb5d783bee1638e84697Adrian Roos } 19994e15a59b757678949cccb5d783bee1638e84697Adrian Roos if (sb.length() == 0) { 20094e15a59b757678949cccb5d783bee1638e84697Adrian Roos sb.append('0'); 20194e15a59b757678949cccb5d783bee1638e84697Adrian Roos } 20294e15a59b757678949cccb5d783bee1638e84697Adrian Roos return sb.toString(); 20394e15a59b757678949cccb5d783bee1638e84697Adrian Roos } 2047a4f3d448b17b4bea190c906d7ecc7f8bec9ff80Adrian Roos} 205