/* * 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. *
* The authentication policy should be added as a parameter when calling * {@link KeyChain#createManageCredentialsIntent}. *
* Example: *
{@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); * }*
*/ 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. *
* appPackageName -> uri -> alias
*/
@NonNull
private final Map
* If this method is called with a package name and URI that was previously added, the
* previous alias will be overwritten.
*
* 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.
*
* 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
* appPackageName -> uri -> alias
*/
@NonNull
public Map