1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
|
/*
* Copyright (C) 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.security;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.app.Activity;
import android.net.Uri;
import android.os.Parcel;
import android.os.Parcelable;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlSerializer;
import java.io.IOException;
import java.security.Principal;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
/**
* The app-URI authentication policy is set by the credential management app. This policy determines
* which alias for a private key and certificate pair should be used for authentication.
* <p>
* The authentication policy should be added as a parameter when calling
* {@link KeyChain#createManageCredentialsIntent}.
* <p>
* Example:
* <pre>{@code
* AppUriAuthenticationPolicy authenticationPolicy = new AppUriAuthenticationPolicy.Builder()
* .addAppAndUriMapping("com.test.pkg", testUri, "testAlias")
* .addAppAndUriMapping("com.test2.pkg", testUri1, "testAlias2")
* .addAppAndUriMapping("com.test2.pkg", testUri2, "testAlias2")
* .build();
* Intent requestIntent = KeyChain.createManageCredentialsIntent(authenticationPolicy);
* }</pre>
* <p>
*/
public final class AppUriAuthenticationPolicy implements Parcelable {
private static final String KEY_AUTHENTICATION_POLICY_APP_TO_URIS =
"authentication_policy_app_to_uris";
private static final String KEY_AUTHENTICATION_POLICY_APP = "policy_app";
/**
* The mappings from an app and list of URIs to a list of aliases, which will be used for
* authentication.
* <p>
* appPackageName -> uri -> alias
*/
@NonNull
private final Map<String, UrisToAliases> mAppToUris;
private AppUriAuthenticationPolicy(@NonNull Map<String, UrisToAliases> appToUris) {
Objects.requireNonNull(appToUris);
this.mAppToUris = appToUris;
}
/**
* Builder class for {@link AppUriAuthenticationPolicy} objects.
*/
public static final class Builder {
private Map<String, UrisToAliases> mPackageNameToUris;
/**
* Initialize a new Builder to construct an {@link AppUriAuthenticationPolicy}.
*/
public Builder() {
mPackageNameToUris = new HashMap<>();
}
/**
* Adds mappings from an app and URI to an alias, which will be used for authentication.
* <p>
* If this method is called with a package name and URI that was previously added, the
* previous alias will be overwritten.
* <p>
* When the system tries to determine which alias to return to a requesting app calling
* {@code KeyChain.choosePrivateKeyAlias}, it will choose the alias whose associated URI
* exactly matches the URI provided in {@link KeyChain#choosePrivateKeyAlias(
* Activity, KeyChainAliasCallback, String[], Principal[], Uri, String)} or the URI
* built from the host and port provided in {@link KeyChain#choosePrivateKeyAlias(
* Activity, KeyChainAliasCallback, String[], Principal[], String, int, String)}.
*
* @param appPackageName The app's package name to authenticate the user to.
* @param uri The URI to authenticate the user to.
* @param alias The alias which will be used for authentication.
*
* @return the same Builder instance.
*/
@NonNull
public Builder addAppAndUriMapping(@NonNull String appPackageName, @NonNull Uri uri,
@NonNull String alias) {
Objects.requireNonNull(appPackageName);
Objects.requireNonNull(uri);
Objects.requireNonNull(alias);
UrisToAliases urisToAliases =
mPackageNameToUris.getOrDefault(appPackageName, new UrisToAliases());
urisToAliases.addUriToAlias(uri, alias);
mPackageNameToUris.put(appPackageName, urisToAliases);
return this;
}
/**
* Adds mappings from an app and list of URIs to a list of aliases, which will be used for
* authentication.
* <p>
* appPackageName -> uri -> alias
*
* @hide
*/
@NonNull
public Builder addAppAndUriMapping(@NonNull String appPackageName,
@NonNull UrisToAliases urisToAliases) {
Objects.requireNonNull(appPackageName);
Objects.requireNonNull(urisToAliases);
mPackageNameToUris.put(appPackageName, urisToAliases);
return this;
}
/**
* Combines all of the attributes that have been set on the {@link Builder}
*
* @return a new {@link AppUriAuthenticationPolicy} object.
*/
@NonNull
public AppUriAuthenticationPolicy build() {
return new AppUriAuthenticationPolicy(mPackageNameToUris);
}
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(@NonNull Parcel dest, int flags) {
dest.writeMap(mAppToUris);
}
@NonNull
public static final Parcelable.Creator<AppUriAuthenticationPolicy> CREATOR =
new Parcelable.Creator<AppUriAuthenticationPolicy>() {
@Override
public AppUriAuthenticationPolicy createFromParcel(Parcel in) {
Map<String, UrisToAliases> appToUris = new HashMap<>();
in.readMap(appToUris, UrisToAliases.class.getClassLoader());
return new AppUriAuthenticationPolicy(appToUris);
}
@Override
public AppUriAuthenticationPolicy[] newArray(int size) {
return new AppUriAuthenticationPolicy[size];
}
};
@Override
public String toString() {
return "AppUriAuthenticationPolicy{"
+ "mPackageNameToUris=" + mAppToUris
+ '}';
}
/**
* Return the authentication policy mapping, which determines which alias for a private key
* and certificate pair should be used for authentication.
* <p>
* appPackageName -> uri -> alias
*/
@NonNull
public Map<String, Map<Uri, String>> getAppAndUriMappings() {
Map<String, Map<Uri, String>> appAndUris = new HashMap<>();
for (Map.Entry<String, UrisToAliases> entry : mAppToUris.entrySet()) {
appAndUris.put(entry.getKey(), entry.getValue().getUrisToAliases());
}
return appAndUris;
}
/**
* Restore a previously saved {@link AppUriAuthenticationPolicy} from XML.
*
* @hide
*/
@Nullable
public static AppUriAuthenticationPolicy readFromXml(@NonNull XmlPullParser parser)
throws IOException, XmlPullParserException {
AppUriAuthenticationPolicy.Builder builder = new AppUriAuthenticationPolicy.Builder();
int outerDepth = parser.getDepth();
int type;
while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
&& (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
continue;
}
if (!parser.getName().equals(KEY_AUTHENTICATION_POLICY_APP_TO_URIS)) {
continue;
}
String app = parser.getAttributeValue(null, KEY_AUTHENTICATION_POLICY_APP);
UrisToAliases urisToAliases = UrisToAliases.readFromXml(parser);
builder.addAppAndUriMapping(app, urisToAliases);
}
return builder.build();
}
/**
* Save the {@link AppUriAuthenticationPolicy} to XML.
*
* @hide
*/
public void writeToXml(@NonNull XmlSerializer out) throws IOException {
for (Map.Entry<String, UrisToAliases> appsToUris : mAppToUris.entrySet()) {
out.startTag(null, KEY_AUTHENTICATION_POLICY_APP_TO_URIS);
out.attribute(null, KEY_AUTHENTICATION_POLICY_APP, appsToUris.getKey());
appsToUris.getValue().writeToXml(out);
out.endTag(null, KEY_AUTHENTICATION_POLICY_APP_TO_URIS);
}
}
/**
* Get the set of aliases found in the policy.
*
* @hide
*/
public Set<String> getAliases() {
Set<String> aliases = new HashSet<>();
for (UrisToAliases appsToUris : mAppToUris.values()) {
aliases.addAll(appsToUris.getUrisToAliases().values());
}
return aliases;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof AppUriAuthenticationPolicy)) {
return false;
}
AppUriAuthenticationPolicy other = (AppUriAuthenticationPolicy) obj;
return Objects.equals(mAppToUris, other.mAppToUris);
}
@Override
public int hashCode() {
return mAppToUris.hashCode();
}
}
|