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 android.telephony; 18 19 import android.compat.annotation.UnsupportedAppUsage; 20 import android.os.Build; 21 import android.text.TextUtils; 22 import android.util.Base64; 23 import android.util.Log; 24 25 import java.security.MessageDigest; 26 import java.security.NoSuchAlgorithmException; 27 28 29 /** 30 * A class to log strings to the RADIO LOG. 31 * 32 * @hide 33 */ 34 public final class Rlog { 35 36 private static final boolean USER_BUILD = Build.IS_USER; 37 Rlog()38 private Rlog() { 39 } 40 41 @UnsupportedAppUsage v(String tag, String msg)42 public static int v(String tag, String msg) { 43 return Log.println_native(Log.LOG_ID_RADIO, Log.VERBOSE, tag, msg); 44 } 45 v(String tag, String msg, Throwable tr)46 public static int v(String tag, String msg, Throwable tr) { 47 return Log.println_native(Log.LOG_ID_RADIO, Log.VERBOSE, tag, 48 msg + '\n' + Log.getStackTraceString(tr)); 49 } 50 51 @UnsupportedAppUsage d(String tag, String msg)52 public static int d(String tag, String msg) { 53 return Log.println_native(Log.LOG_ID_RADIO, Log.DEBUG, tag, msg); 54 } 55 56 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) d(String tag, String msg, Throwable tr)57 public static int d(String tag, String msg, Throwable tr) { 58 return Log.println_native(Log.LOG_ID_RADIO, Log.DEBUG, tag, 59 msg + '\n' + Log.getStackTraceString(tr)); 60 } 61 62 @UnsupportedAppUsage i(String tag, String msg)63 public static int i(String tag, String msg) { 64 return Log.println_native(Log.LOG_ID_RADIO, Log.INFO, tag, msg); 65 } 66 67 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) i(String tag, String msg, Throwable tr)68 public static int i(String tag, String msg, Throwable tr) { 69 return Log.println_native(Log.LOG_ID_RADIO, Log.INFO, tag, 70 msg + '\n' + Log.getStackTraceString(tr)); 71 } 72 73 @UnsupportedAppUsage w(String tag, String msg)74 public static int w(String tag, String msg) { 75 return Log.println_native(Log.LOG_ID_RADIO, Log.WARN, tag, msg); 76 } 77 78 @UnsupportedAppUsage w(String tag, String msg, Throwable tr)79 public static int w(String tag, String msg, Throwable tr) { 80 return Log.println_native(Log.LOG_ID_RADIO, Log.WARN, tag, 81 msg + '\n' + Log.getStackTraceString(tr)); 82 } 83 w(String tag, Throwable tr)84 public static int w(String tag, Throwable tr) { 85 return Log.println_native(Log.LOG_ID_RADIO, Log.WARN, tag, Log.getStackTraceString(tr)); 86 } 87 88 @UnsupportedAppUsage e(String tag, String msg)89 public static int e(String tag, String msg) { 90 return Log.println_native(Log.LOG_ID_RADIO, Log.ERROR, tag, msg); 91 } 92 93 @UnsupportedAppUsage e(String tag, String msg, Throwable tr)94 public static int e(String tag, String msg, Throwable tr) { 95 return Log.println_native(Log.LOG_ID_RADIO, Log.ERROR, tag, 96 msg + '\n' + Log.getStackTraceString(tr)); 97 } 98 println(int priority, String tag, String msg)99 public static int println(int priority, String tag, String msg) { 100 return Log.println_native(Log.LOG_ID_RADIO, priority, tag, msg); 101 } 102 isLoggable(String tag, int level)103 public static boolean isLoggable(String tag, int level) { 104 return Log.isLoggable(tag, level); 105 } 106 107 /** 108 * Redact personally identifiable information for production users. 109 * @param tag used to identify the source of a log message 110 * @param pii the personally identifiable information we want to apply secure hash on. 111 * @return If tag is loggable in verbose mode or pii is null, return the original input. 112 * otherwise return a secure Hash of input pii 113 */ pii(String tag, Object pii)114 public static String pii(String tag, Object pii) { 115 String val = String.valueOf(pii); 116 if (pii == null || TextUtils.isEmpty(val) || isLoggable(tag, Log.VERBOSE)) { 117 return val; 118 } 119 return "[" + secureHash(val.getBytes()) + "]"; 120 } 121 122 /** 123 * Redact personally identifiable information for production users. 124 * @param enablePiiLogging set when caller explicitly want to enable sensitive logging. 125 * @param pii the personally identifiable information we want to apply secure hash on. 126 * @return If enablePiiLogging is set to true or pii is null, return the original input. 127 * otherwise return a secure Hash of input pii 128 */ pii(boolean enablePiiLogging, Object pii)129 public static String pii(boolean enablePiiLogging, Object pii) { 130 String val = String.valueOf(pii); 131 if (pii == null || TextUtils.isEmpty(val) || enablePiiLogging) { 132 return val; 133 } 134 return "[" + secureHash(val.getBytes()) + "]"; 135 } 136 137 /** 138 * Returns a secure hash (using the SHA1 algorithm) of the provided input. 139 * 140 * @return "****" if the build type is user, otherwise the hash 141 * @param input the bytes for which the secure hash should be computed. 142 */ secureHash(byte[] input)143 private static String secureHash(byte[] input) { 144 // Refrain from logging user personal information in user build. 145 if (USER_BUILD) { 146 return "****"; 147 } 148 149 MessageDigest messageDigest; 150 151 try { 152 messageDigest = MessageDigest.getInstance("SHA-1"); 153 } catch (NoSuchAlgorithmException e) { 154 return "####"; 155 } 156 157 byte[] result = messageDigest.digest(input); 158 return Base64.encodeToString( 159 result, Base64.URL_SAFE | Base64.NO_PADDING | Base64.NO_WRAP); 160 } 161 } 162 163