IpSecManager and IpSecAlgorithm API Tweaks

-Add a reserveSecurityParamterIndex() function that allows the
 system to select an SPI.
-Disallow INVALID_SECURITY_PARAMETER_INDEX from being passed as
 an explicit SPI request.
-Remove the ALGO_ prefix from constants in IpSecAlgorithm

Bug: 36073210
Test: Updated CTS tests still pass on bullhead
Change-Id: Ic94809996076b0718f153f550b82192fe7048a2e
This commit is contained in:
Nathan Harold
2017-04-06 18:16:28 -07:00
parent 7c30d6b8f2
commit ed0fcf1faf
2 changed files with 47 additions and 18 deletions

View File

@@ -32,7 +32,7 @@ public final class IpSecAlgorithm implements Parcelable {
* *
* <p>Valid lengths for this key are {128, 192, 256}. * <p>Valid lengths for this key are {128, 192, 256}.
*/ */
public static final String ALGO_CRYPT_AES_CBC = "cbc(aes)"; public static final String CRYPT_AES_CBC = "cbc(aes)";
/** /**
* MD5 HMAC Authentication/Integrity Algorithm. This algorithm is not recommended for use in new * MD5 HMAC Authentication/Integrity Algorithm. This algorithm is not recommended for use in new
@@ -40,7 +40,7 @@ public final class IpSecAlgorithm implements Parcelable {
* *
* <p>Valid truncation lengths are multiples of 8 bits from 96 to (default) 128. * <p>Valid truncation lengths are multiples of 8 bits from 96 to (default) 128.
*/ */
public static final String ALGO_AUTH_HMAC_MD5 = "hmac(md5)"; public static final String AUTH_HMAC_MD5 = "hmac(md5)";
/** /**
* SHA1 HMAC Authentication/Integrity Algorithm. This algorithm is not recommended for use in * SHA1 HMAC Authentication/Integrity Algorithm. This algorithm is not recommended for use in
@@ -48,35 +48,35 @@ public final class IpSecAlgorithm implements Parcelable {
* *
* <p>Valid truncation lengths are multiples of 8 bits from 96 to (default) 160. * <p>Valid truncation lengths are multiples of 8 bits from 96 to (default) 160.
*/ */
public static final String ALGO_AUTH_HMAC_SHA1 = "hmac(sha1)"; public static final String AUTH_HMAC_SHA1 = "hmac(sha1)";
/** /**
* SHA256 HMAC Authentication/Integrity Algorithm. * SHA256 HMAC Authentication/Integrity Algorithm.
* *
* <p>Valid truncation lengths are multiples of 8 bits from 96 to (default) 256. * <p>Valid truncation lengths are multiples of 8 bits from 96 to (default) 256.
*/ */
public static final String ALGO_AUTH_HMAC_SHA256 = "hmac(sha256)"; public static final String AUTH_HMAC_SHA256 = "hmac(sha256)";
/** /**
* SHA384 HMAC Authentication/Integrity Algorithm. * SHA384 HMAC Authentication/Integrity Algorithm.
* *
* <p>Valid truncation lengths are multiples of 8 bits from 192 to (default) 384. * <p>Valid truncation lengths are multiples of 8 bits from 192 to (default) 384.
*/ */
public static final String ALGO_AUTH_HMAC_SHA384 = "hmac(sha384)"; public static final String AUTH_HMAC_SHA384 = "hmac(sha384)";
/** /**
* SHA512 HMAC Authentication/Integrity Algorithm * SHA512 HMAC Authentication/Integrity Algorithm
* *
* <p>Valid truncation lengths are multiples of 8 bits from 256 to (default) 512. * <p>Valid truncation lengths are multiples of 8 bits from 256 to (default) 512.
*/ */
public static final String ALGO_AUTH_HMAC_SHA512 = "hmac(sha512)"; public static final String AUTH_HMAC_SHA512 = "hmac(sha512)";
/** @hide */ /** @hide */
@StringDef({ @StringDef({
ALGO_CRYPT_AES_CBC, CRYPT_AES_CBC,
ALGO_AUTH_HMAC_MD5, AUTH_HMAC_MD5,
ALGO_AUTH_HMAC_SHA1, AUTH_HMAC_SHA1,
ALGO_AUTH_HMAC_SHA256, AUTH_HMAC_SHA256,
ALGO_AUTH_HMAC_SHA512 AUTH_HMAC_SHA512
}) })
@Retention(RetentionPolicy.SOURCE) @Retention(RetentionPolicy.SOURCE)
public @interface AlgorithmName {} public @interface AlgorithmName {}
@@ -164,17 +164,17 @@ public final class IpSecAlgorithm implements Parcelable {
private static boolean isTruncationLengthValid(String algo, int truncLenBits) { private static boolean isTruncationLengthValid(String algo, int truncLenBits) {
switch (algo) { switch (algo) {
case ALGO_CRYPT_AES_CBC: case CRYPT_AES_CBC:
return (truncLenBits == 128 || truncLenBits == 192 || truncLenBits == 256); return (truncLenBits == 128 || truncLenBits == 192 || truncLenBits == 256);
case ALGO_AUTH_HMAC_MD5: case AUTH_HMAC_MD5:
return (truncLenBits >= 96 && truncLenBits <= 128); return (truncLenBits >= 96 && truncLenBits <= 128);
case ALGO_AUTH_HMAC_SHA1: case AUTH_HMAC_SHA1:
return (truncLenBits >= 96 && truncLenBits <= 160); return (truncLenBits >= 96 && truncLenBits <= 160);
case ALGO_AUTH_HMAC_SHA256: case AUTH_HMAC_SHA256:
return (truncLenBits >= 96 && truncLenBits <= 256); return (truncLenBits >= 96 && truncLenBits <= 256);
case ALGO_AUTH_HMAC_SHA384: case AUTH_HMAC_SHA384:
return (truncLenBits >= 192 && truncLenBits <= 384); return (truncLenBits >= 192 && truncLenBits <= 384);
case ALGO_AUTH_HMAC_SHA512: case AUTH_HMAC_SHA512:
return (truncLenBits >= 256 && truncLenBits <= 512); return (truncLenBits >= 256 && truncLenBits <= 512);
default: default:
return false; return false;

View File

@@ -193,15 +193,44 @@ public final class IpSecManager {
* *
* @param direction {@link IpSecTransform#DIRECTION_IN} or {@link IpSecTransform#DIRECTION_OUT} * @param direction {@link IpSecTransform#DIRECTION_IN} or {@link IpSecTransform#DIRECTION_OUT}
* @param remoteAddress address of the remote. SPIs must be unique for each remoteAddress. * @param remoteAddress address of the remote. SPIs must be unique for each remoteAddress.
* @param requestedSpi the requested SPI, or '0' to allocate a random SPI.
* @return the reserved SecurityParameterIndex * @return the reserved SecurityParameterIndex
* @throws ResourceUnavailableException indicating that too many SPIs are currently allocated * @throws ResourceUnavailableException indicating that too many SPIs are currently allocated
* for this user * for this user
* @throws SpiUnavailableException indicating that a particular SPI cannot be reserved * @throws SpiUnavailableException indicating that a particular SPI cannot be reserved
*/ */
public SecurityParameterIndex reserveSecurityParameterIndex(
int direction, InetAddress remoteAddress)
throws ResourceUnavailableException {
try {
return new SecurityParameterIndex(
mService,
direction,
remoteAddress,
IpSecManager.INVALID_SECURITY_PARAMETER_INDEX);
} catch (SpiUnavailableException unlikely) {
throw new ResourceUnavailableException("No SPIs available");
}
}
/**
* Reserve an SPI for traffic bound towards the specified remote address.
*
* <p>If successful, this SPI is guaranteed available until released by a call to {@link
* SecurityParameterIndex#close()}.
*
* @param direction {@link IpSecTransform#DIRECTION_IN} or {@link IpSecTransform#DIRECTION_OUT}
* @param remoteAddress address of the remote. SPIs must be unique for each remoteAddress.
* @param requestedSpi the requested SPI, or '0' to allocate a random SPI.
* @return the reserved SecurityParameterIndex
* @throws ResourceUnavailableException indicating that too many SPIs are currently allocated
* for this user
*/
public SecurityParameterIndex reserveSecurityParameterIndex( public SecurityParameterIndex reserveSecurityParameterIndex(
int direction, InetAddress remoteAddress, int requestedSpi) int direction, InetAddress remoteAddress, int requestedSpi)
throws SpiUnavailableException, ResourceUnavailableException { throws SpiUnavailableException, ResourceUnavailableException {
if (requestedSpi == IpSecManager.INVALID_SECURITY_PARAMETER_INDEX) {
throw new IllegalArgumentException("Requested SPI must be a valid (non-zero) SPI");
}
return new SecurityParameterIndex(mService, direction, remoteAddress, requestedSpi); return new SecurityParameterIndex(mService, direction, remoteAddress, requestedSpi);
} }