diff options
| author | 2018-03-30 20:28:19 +0000 | |
|---|---|---|
| committer | 2018-03-30 20:28:19 +0000 | |
| commit | 36f2d8a4e1247b4556d10691eb8e40b26122f92c (patch) | |
| tree | ac32f575ac66f558b48561171a8171d5df66e4ec | |
| parent | eff51d566a78efbf6e44fa67bef6689e261079ac (diff) | |
| parent | bfa67c81bf88b842fdb63b63b0cfd96e021f82af (diff) | |
Merge changes from topic "encap-api" into pi-dev
* changes:
Require explicitly supplied truncation length
Clarify UDP encapsulation socket API
| -rw-r--r-- | api/current.txt | 2 | ||||
| -rw-r--r-- | core/java/android/net/IpSecAlgorithm.java | 22 | ||||
| -rw-r--r-- | core/java/android/net/IpSecManager.java | 6 | ||||
| -rw-r--r-- | services/core/java/com/android/server/IpSecService.java | 2 | ||||
| -rw-r--r-- | tests/net/java/android/net/IpSecAlgorithmTest.java | 38 | ||||
| -rw-r--r-- | tests/net/java/android/net/IpSecConfigTest.java | 3 | ||||
| -rw-r--r-- | tests/net/java/android/net/IpSecManagerTest.java | 4 |
7 files changed, 51 insertions, 26 deletions
diff --git a/api/current.txt b/api/current.txt index 11760ef1e864..8090aa02c960 100644 --- a/api/current.txt +++ b/api/current.txt @@ -27013,8 +27013,8 @@ package android.net { public static final class IpSecManager.UdpEncapsulationSocket implements java.lang.AutoCloseable { method public void close() throws java.io.IOException; + method public java.io.FileDescriptor getFileDescriptor(); method public int getPort(); - method public java.io.FileDescriptor getSocket(); } public final class IpSecTransform implements java.lang.AutoCloseable { diff --git a/core/java/android/net/IpSecAlgorithm.java b/core/java/android/net/IpSecAlgorithm.java index 57f05884ce6d..8034bb62c94c 100644 --- a/core/java/android/net/IpSecAlgorithm.java +++ b/core/java/android/net/IpSecAlgorithm.java @@ -56,7 +56,8 @@ public final class IpSecAlgorithm implements Parcelable { * new applications and is provided for legacy compatibility with 3gpp infrastructure.</b> * * <p>Keys for this algorithm must be 128 bits in length. - * <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 128. */ public static final String AUTH_HMAC_MD5 = "hmac(md5)"; @@ -65,7 +66,8 @@ public final class IpSecAlgorithm implements Parcelable { * new applications and is provided for legacy compatibility with 3gpp infrastructure.</b> * * <p>Keys for this algorithm must be 160 bits in length. - * <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 160. */ public static final String AUTH_HMAC_SHA1 = "hmac(sha1)"; @@ -73,7 +75,8 @@ public final class IpSecAlgorithm implements Parcelable { * SHA256 HMAC Authentication/Integrity Algorithm. * * <p>Keys for this algorithm must be 256 bits in length. - * <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 256. */ public static final String AUTH_HMAC_SHA256 = "hmac(sha256)"; @@ -81,7 +84,8 @@ public final class IpSecAlgorithm implements Parcelable { * SHA384 HMAC Authentication/Integrity Algorithm. * * <p>Keys for this algorithm must be 384 bits in length. - * <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 384. */ public static final String AUTH_HMAC_SHA384 = "hmac(sha384)"; @@ -89,7 +93,8 @@ public final class IpSecAlgorithm implements Parcelable { * SHA512 HMAC Authentication/Integrity Algorithm. * * <p>Keys for this algorithm must be 512 bits in length. - * <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 512. */ public static final String AUTH_HMAC_SHA512 = "hmac(sha512)"; @@ -112,6 +117,7 @@ public final class IpSecAlgorithm implements Parcelable { AUTH_HMAC_MD5, AUTH_HMAC_SHA1, AUTH_HMAC_SHA256, + AUTH_HMAC_SHA384, AUTH_HMAC_SHA512, AUTH_CRYPT_AES_GCM }) @@ -126,11 +132,14 @@ public final class IpSecAlgorithm implements Parcelable { * Creates an IpSecAlgorithm of one of the supported types. Supported algorithm names are * defined as constants in this class. * + * <p>For algorithms that produce an integrity check value, the truncation length is a required + * parameter. See {@link #IpSecAlgorithm(String algorithm, byte[] key, int truncLenBits)} + * * @param algorithm name of the algorithm. * @param key key padded to a multiple of 8 bits. */ public IpSecAlgorithm(@NonNull @AlgorithmName String algorithm, @NonNull byte[] key) { - this(algorithm, key, key.length * 8); + this(algorithm, key, 0); } /** @@ -228,6 +237,7 @@ public final class IpSecAlgorithm implements Parcelable { case AUTH_CRYPT_AES_GCM: // The keying material for GCM is a key plus a 32-bit salt isValidLen = keyLen == 128 + 32 || keyLen == 192 + 32 || keyLen == 256 + 32; + isValidTruncLen = truncLen == 64 || truncLen == 96 || truncLen == 128; break; default: throw new IllegalArgumentException("Couldn't find an algorithm: " + name); diff --git a/core/java/android/net/IpSecManager.java b/core/java/android/net/IpSecManager.java index a88fe0428a06..c7234e316536 100644 --- a/core/java/android/net/IpSecManager.java +++ b/core/java/android/net/IpSecManager.java @@ -502,7 +502,7 @@ public final class IpSecManager { * signalling and UDP encapsulated IPsec traffic. Instances can be obtained by calling {@link * IpSecManager#openUdpEncapsulationSocket}. The provided socket cannot be re-bound by the * caller. The caller should not close the {@code FileDescriptor} returned by {@link - * #getSocket}, but should use {@link #close} instead. + * #getFileDescriptor}, but should use {@link #close} instead. * * <p>Allowing the user to close or unbind a UDP encapsulation socket could impact the traffic * of the next user who binds to that port. To prevent this scenario, these sockets are held @@ -541,8 +541,8 @@ public final class IpSecManager { mCloseGuard.open("constructor"); } - /** Get the wrapped socket. */ - public FileDescriptor getSocket() { + /** Get the encapsulation socket's file descriptor. */ + public FileDescriptor getFileDescriptor() { if (mPfd == null) { return null; } diff --git a/services/core/java/com/android/server/IpSecService.java b/services/core/java/com/android/server/IpSecService.java index d09a161d1ef4..06c10564ab98 100644 --- a/services/core/java/com/android/server/IpSecService.java +++ b/services/core/java/com/android/server/IpSecService.java @@ -931,7 +931,7 @@ public class IpSecService extends IIpSecService.Stub { return mPort; } - public FileDescriptor getSocket() { + public FileDescriptor getFileDescriptor() { return mSocket; } diff --git a/tests/net/java/android/net/IpSecAlgorithmTest.java b/tests/net/java/android/net/IpSecAlgorithmTest.java index 6bdfdc6db2d1..85e836179b5e 100644 --- a/tests/net/java/android/net/IpSecAlgorithmTest.java +++ b/tests/net/java/android/net/IpSecAlgorithmTest.java @@ -22,8 +22,12 @@ import static org.junit.Assert.fail; import android.os.Parcel; import android.support.test.filters.SmallTest; import android.support.test.runner.AndroidJUnit4; + +import java.util.AbstractMap.SimpleEntry; import java.util.Arrays; +import java.util.Map.Entry; import java.util.Random; + import org.junit.Test; import org.junit.runner.RunWith; @@ -40,19 +44,29 @@ public class IpSecAlgorithmTest { }; @Test - public void testDefaultTruncLen() throws Exception { - IpSecAlgorithm explicit = - new IpSecAlgorithm( - IpSecAlgorithm.AUTH_HMAC_SHA256, Arrays.copyOf(KEY_MATERIAL, 256 / 8), 256); - IpSecAlgorithm implicit = + public void testNoTruncLen() throws Exception { + Entry<String, Integer>[] authAndAeadList = + new Entry[] { + new SimpleEntry<>(IpSecAlgorithm.AUTH_HMAC_MD5, 128), + new SimpleEntry<>(IpSecAlgorithm.AUTH_HMAC_SHA1, 160), + new SimpleEntry<>(IpSecAlgorithm.AUTH_HMAC_SHA256, 256), + new SimpleEntry<>(IpSecAlgorithm.AUTH_HMAC_SHA384, 384), + new SimpleEntry<>(IpSecAlgorithm.AUTH_HMAC_SHA512, 512), + new SimpleEntry<>(IpSecAlgorithm.AUTH_CRYPT_AES_GCM, 224) + }; + + // Expect auth and aead algorithms to throw errors if trunclen is omitted. + for (Entry<String, Integer> algData : authAndAeadList) { + try { new IpSecAlgorithm( - IpSecAlgorithm.AUTH_HMAC_SHA256, Arrays.copyOf(KEY_MATERIAL, 256 / 8)); - assertTrue( - "Default Truncation Length Incorrect, Explicit: " - + explicit - + "implicit: " - + implicit, - IpSecAlgorithm.equals(explicit, implicit)); + algData.getKey(), Arrays.copyOf(KEY_MATERIAL, algData.getValue() / 8)); + fail("Expected exception on unprovided auth trunclen"); + } catch (IllegalArgumentException expected) { + } + } + + // Ensure crypt works with no truncation length supplied. + new IpSecAlgorithm(IpSecAlgorithm.CRYPT_AES_CBC, Arrays.copyOf(KEY_MATERIAL, 256 / 8)); } @Test diff --git a/tests/net/java/android/net/IpSecConfigTest.java b/tests/net/java/android/net/IpSecConfigTest.java index f186ee55d2c7..771faaf4955d 100644 --- a/tests/net/java/android/net/IpSecConfigTest.java +++ b/tests/net/java/android/net/IpSecConfigTest.java @@ -62,7 +62,8 @@ public class IpSecConfigTest { c.setAuthentication( new IpSecAlgorithm( IpSecAlgorithm.AUTH_HMAC_MD5, - new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF, 0})); + new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF, 0}, + 128)); c.setAuthenticatedEncryption( new IpSecAlgorithm( IpSecAlgorithm.AUTH_CRYPT_AES_GCM, diff --git a/tests/net/java/android/net/IpSecManagerTest.java b/tests/net/java/android/net/IpSecManagerTest.java index 0ca20dee427f..970596d6b0f3 100644 --- a/tests/net/java/android/net/IpSecManagerTest.java +++ b/tests/net/java/android/net/IpSecManagerTest.java @@ -179,7 +179,7 @@ public class IpSecManagerTest { IpSecManager.UdpEncapsulationSocket encapSocket = mIpSecManager.openUdpEncapsulationSocket(TEST_UDP_ENCAP_PORT); - assertNotNull(encapSocket.getSocket()); + assertNotNull(encapSocket.getFileDescriptor()); assertEquals(TEST_UDP_ENCAP_PORT, encapSocket.getPort()); encapSocket.close(); @@ -202,7 +202,7 @@ public class IpSecManagerTest { IpSecManager.UdpEncapsulationSocket encapSocket = mIpSecManager.openUdpEncapsulationSocket(); - assertNotNull(encapSocket.getSocket()); + assertNotNull(encapSocket.getFileDescriptor()); assertEquals(TEST_UDP_ENCAP_PORT, encapSocket.getPort()); encapSocket.close(); |