Skip to content

Commit

Permalink
8296741: Illegal X400Address and EDIPartyName should not be created
Browse files Browse the repository at this point in the history
Reviewed-by: xuelei, valeriep
  • Loading branch information
wangweij committed Nov 14, 2022
1 parent b0edfc1 commit e1d298c
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 12 deletions.
12 changes: 7 additions & 5 deletions src/java.base/share/classes/sun/security/x509/EDIPartyName.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
package sun.security.x509;

import java.io.IOException;
import java.util.Objects;

import sun.security.util.*;

/**
Expand Down Expand Up @@ -61,7 +63,7 @@ public class EDIPartyName implements GeneralNameInterface {
*/
public EDIPartyName(String assignerName, String partyName) {
this.assigner = assignerName;
this.party = partyName;
this.party = Objects.requireNonNull(partyName);
}

/**
Expand All @@ -70,7 +72,7 @@ public EDIPartyName(String assignerName, String partyName) {
* @param partyName the name of the EDI party.
*/
public EDIPartyName(String partyName) {
this.party = partyName;
this(null, partyName);
}

/**
Expand Down Expand Up @@ -106,6 +108,9 @@ public EDIPartyName(DerValue derValue) throws IOException {
party = opt.getAsString();
}
}
if (party == null) {
throw new IOException("party cannot be missing");
}
}

/**
Expand All @@ -132,9 +137,6 @@ public void encode(DerOutputStream out) throws IOException {
tagged.write(DerValue.createTag(DerValue.TAG_CONTEXT,
false, TAG_ASSIGNER), tmp2);
}
if (party == null)
throw new IOException("Cannot have null partyName");

// XXX - shd check is chars fit into PrintableString
tmp.putPrintableString(party);
tagged.write(DerValue.createTag(DerValue.TAG_CONTEXT,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ private GeneralSubtree createWidestSubtree(GeneralNameInterface name) {
newName = new GeneralName(new DNSName(""));
break;
case GeneralNameInterface.NAME_X400:
newName = new GeneralName(new X400Address((byte[])null));
newName = new GeneralName(new X400Address(null));
break;
case GeneralNameInterface.NAME_DIRECTORY:
newName = new GeneralName(new X500Name(""));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,16 +335,13 @@
public class X400Address implements GeneralNameInterface {

// Private data members
byte[] nameValue;
DerValue derValue;

/**
* Create the X400Address object from the specified byte array
*
* @param value value of the name as a byte array
*/
public X400Address(byte[] value) {
nameValue = value;
}

/**
* Create the X400Address object from the passed encoded Der value.
Expand All @@ -353,7 +350,7 @@ public X400Address(byte[] value) {
* @exception IOException on error.
*/
public X400Address(DerValue derValue) throws IOException {
nameValue = derValue.toByteArray();
this.derValue = derValue;
}

/**
Expand All @@ -369,8 +366,8 @@ public int getType() {
* @param out the DER stream to encode the X400Address to.
* @exception IOException on encoding errors.
*/
@Override
public void encode(DerOutputStream out) throws IOException {
DerValue derValue = new DerValue(nameValue);
out.putDerValue(derValue);
}

Expand Down
48 changes: 48 additions & 0 deletions test/jdk/sun/security/x509/EDIPartyName/NullName.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

/* @test
* @bug 8296741
* @summary Illegal X400Address and EDIPartyName should not be created
* @library /test/lib
* @modules java.base/sun.security.x509
*/

import jdk.test.lib.Utils;
import sun.security.x509.EDIPartyName;

public class NullName {

public static void main(String[] argv) throws Exception {
Utils.runAndCheckException(
() -> new EDIPartyName((String)null),
NullPointerException.class);
Utils.runAndCheckException(
() -> new EDIPartyName(null, null),
NullPointerException.class);
Utils.runAndCheckException(
() -> new EDIPartyName("hello", null),
NullPointerException.class);
new EDIPartyName(null, "hello");
}
}

1 comment on commit e1d298c

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.