Skip to content

Commit 2a243a3

Browse files
committed
8267617: Certificate's IP x509 NameConstraints raises ArrayIndexOutOfBoundsException
Reviewed-by: mullan
1 parent 923c746 commit 2a243a3

File tree

2 files changed

+86
-5
lines changed

2 files changed

+86
-5
lines changed

src/java.base/share/classes/sun/security/x509/IPAddressName.java

+8-5
Original file line numberDiff line numberDiff line change
@@ -401,11 +401,12 @@ else if (inputName.equals(this))
401401
else {
402402
IPAddressName otherName = (IPAddressName)inputName;
403403
byte[] otherAddress = otherName.address;
404-
if (otherAddress.length == 4 && address.length == 4)
404+
if ((otherAddress.length == 4 && address.length == 4) ||
405+
(otherAddress.length == 16 && address.length == 16)) {
405406
// Two host addresses
406407
constraintType = NAME_SAME_TYPE;
407-
else if ((otherAddress.length == 8 && address.length == 8) ||
408-
(otherAddress.length == 32 && address.length == 32)) {
408+
} else if ((otherAddress.length == 8 && address.length == 8) ||
409+
(otherAddress.length == 32 && address.length == 32)) {
409410
// Two subnet addresses
410411
// See if one address fully encloses the other address
411412
boolean otherSubsetOfThis = true;
@@ -440,7 +441,8 @@ else if (thisSubsetOfOther)
440441
constraintType = NAME_WIDENS;
441442
else
442443
constraintType = NAME_SAME_TYPE;
443-
} else if (otherAddress.length == 8 || otherAddress.length == 32) {
444+
} else if ((otherAddress.length == 8 && address.length == 4) ||
445+
(otherAddress.length == 32 && address.length == 16)) {
444446
//Other is a subnet, this is a host address
445447
int i = 0;
446448
int maskOffset = otherAddress.length/2;
@@ -454,7 +456,8 @@ else if (thisSubsetOfOther)
454456
constraintType = NAME_WIDENS;
455457
else
456458
constraintType = NAME_SAME_TYPE;
457-
} else if (address.length == 8 || address.length == 32) {
459+
} else if ((otherAddress.length == 4 && address.length == 8) ||
460+
(otherAddress.length == 16 && address.length == 32)) {
458461
//This is a subnet, other is a host address
459462
int i = 0;
460463
int maskOffset = address.length/2;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
import org.testng.annotations.DataProvider;
25+
import org.testng.annotations.Test;
26+
import sun.security.x509.GeneralNameInterface;
27+
import sun.security.x509.IPAddressName;
28+
29+
import java.io.IOException;
30+
31+
import static org.testng.Assert.assertEquals;
32+
33+
/*
34+
* @test
35+
* @summary Verify IPAddressName.constrains
36+
* @bug 8267617
37+
* @modules java.base/sun.security.x509
38+
* @run testng ConstrainsTest
39+
*/
40+
public class ConstrainsTest {
41+
42+
IPAddressName ipv4Addr = new IPAddressName("127.0.0.1");
43+
IPAddressName ipv4Mask = new IPAddressName("127.0.0.0/255.0.0.0");
44+
IPAddressName ipv6Addr = new IPAddressName("::1");
45+
IPAddressName ipv6Mask = new IPAddressName("::/0");
46+
47+
public ConstrainsTest() throws IOException {
48+
}
49+
50+
@DataProvider(name = "names")
51+
public Object[][] names() {
52+
Object[][] data = {
53+
{ipv4Addr, ipv4Addr, GeneralNameInterface.NAME_MATCH},
54+
{ipv4Addr, ipv4Mask, GeneralNameInterface.NAME_WIDENS},
55+
{ipv4Addr, ipv6Addr, GeneralNameInterface.NAME_SAME_TYPE},
56+
{ipv4Addr, ipv6Mask, GeneralNameInterface.NAME_SAME_TYPE},
57+
{ipv4Mask, ipv4Addr, GeneralNameInterface.NAME_NARROWS},
58+
{ipv4Mask, ipv4Mask, GeneralNameInterface.NAME_MATCH},
59+
{ipv4Mask, ipv6Addr, GeneralNameInterface.NAME_SAME_TYPE},
60+
{ipv4Mask, ipv6Mask, GeneralNameInterface.NAME_SAME_TYPE},
61+
{ipv6Addr, ipv4Addr, GeneralNameInterface.NAME_SAME_TYPE},
62+
{ipv6Addr, ipv4Mask, GeneralNameInterface.NAME_SAME_TYPE},
63+
{ipv6Addr, ipv6Addr, GeneralNameInterface.NAME_MATCH},
64+
{ipv6Addr, ipv6Mask, GeneralNameInterface.NAME_WIDENS},
65+
{ipv6Mask, ipv4Addr, GeneralNameInterface.NAME_SAME_TYPE},
66+
{ipv6Mask, ipv4Mask, GeneralNameInterface.NAME_SAME_TYPE},
67+
{ipv6Mask, ipv6Addr, GeneralNameInterface.NAME_NARROWS},
68+
{ipv6Mask, ipv6Mask, GeneralNameInterface.NAME_MATCH},
69+
};
70+
return data;
71+
}
72+
73+
@Test(dataProvider = "names")
74+
public void testNameContains(IPAddressName addr1, IPAddressName addr2, int result) throws IOException {
75+
assertEquals(addr1.constrains(addr2), result);
76+
}
77+
78+
}

0 commit comments

Comments
 (0)