/* * Copyright (C) 2018 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.net.ipsec.ike; import android.annotation.IntDef; import android.annotation.SystemApi; import android.util.ArraySet; import com.android.internal.net.ipsec.ike.exceptions.AuthenticationFailedException; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.security.cert.CertificateParsingException; import java.security.cert.X509Certificate; import java.util.Collection; import java.util.List; import java.util.Set; /** * IkeIdentification is abstract base class that represents the common information for all types of * IKE entity identification. * *
{@link IkeIdentification} is used in IKE authentication.
*
* @see RFC 7296, Internet Key Exchange
* Protocol Version 2 (IKEv2)
* @hide
*/
@SystemApi
public abstract class IkeIdentification {
// Set of supported ID types.
private static final Set According to RFC 7296, the received IKE ID that types are FQDN, IPv4/IPv6 Address and
* RFC822 Address should match the end certificate Subject Alternative Name (SAN).
*
* @hide
*/
protected void validateEndCertSanOrThrow(
X509Certificate endCert, int expectedSanType, Object expectedSanData)
throws AuthenticationFailedException {
try {
// Each List is one SAN whose first entry is an Integer that represents a SAN type and
// second entry is a String or a byte array that represents the SAN data
Collection> allSans = endCert.getSubjectAlternativeNames();
if (allSans == null) {
throw new AuthenticationFailedException("End certificate does not contain SAN");
}
for (List> san : allSans) {
if ((Integer) san.get(INDEX_SAN_TYPE) == expectedSanType) {
Object item = san.get(INDEX_SAN_DATA);
if (expectedSanData.equals(item)) {
return;
}
}
}
throw new AuthenticationFailedException(
"End certificate SAN and " + getIdTypeString() + " ID mismatched");
} catch (CertificateParsingException e) {
throw new AuthenticationFailedException(e);
}
}
/**
* Return the encoded identification data in a byte array.
*
* @return the encoded identification data.
* @hide
*/
public abstract byte[] getEncodedIdData();
}