Skip to content

Commit a7c2338

Browse files
committed
8296900: CertificateValidity fields are not optional
Reviewed-by: mullan
1 parent 3eb789a commit a7c2338

File tree

2 files changed

+75
-41
lines changed

2 files changed

+75
-41
lines changed

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

+24-41
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.io.IOException;
2828
import java.security.cert.*;
2929
import java.util.Date;
30+
import java.util.Objects;
3031

3132
import sun.security.util.*;
3233

@@ -46,8 +47,8 @@ public class CertificateValidity implements DerEncoder {
4647
static final long YR_2050 = 2524608000000L;
4748

4849
// Private data members
49-
private Date notBefore;
50-
private Date notAfter;
50+
private final Date notBefore;
51+
private final Date notAfter;
5152

5253
// Returns the first time the certificate is valid.
5354
public Date getNotBefore() {
@@ -59,8 +60,27 @@ public Date getNotAfter() {
5960
return new Date(notAfter.getTime());
6061
}
6162

62-
// Construct the class from the DerValue
63-
private void construct(DerValue derVal) throws IOException {
63+
/**
64+
* The constructor for this class for the specified interval.
65+
*
66+
* @param notBefore the date and time before which the certificate
67+
* is not valid
68+
* @param notAfter the date and time after which the certificate is
69+
* not valid
70+
*/
71+
public CertificateValidity(Date notBefore, Date notAfter) {
72+
this.notBefore = Objects.requireNonNull(notBefore);
73+
this.notAfter = Objects.requireNonNull(notAfter);
74+
}
75+
76+
/**
77+
* Create the object, decoding the values from the passed DER stream.
78+
*
79+
* @param in the DerInputStream to read the CertificateValidity from
80+
* @exception IOException on decoding errors.
81+
*/
82+
public CertificateValidity(DerInputStream in) throws IOException {
83+
DerValue derVal = in.getDerValue();
6484
if (derVal.tag != DerValue.tag_Sequence) {
6585
throw new IOException("Invalid encoded CertificateValidity, " +
6686
"starting sequence tag missing.");
@@ -91,41 +111,10 @@ private void construct(DerValue derVal) throws IOException {
91111
}
92112
}
93113

94-
/**
95-
* Default constructor for the class.
96-
*/
97-
public CertificateValidity() { }
98-
99-
/**
100-
* The default constructor for this class for the specified interval.
101-
*
102-
* @param notBefore the date and time before which the certificate
103-
* is not valid.
104-
* @param notAfter the date and time after which the certificate is
105-
* not valid.
106-
*/
107-
public CertificateValidity(Date notBefore, Date notAfter) {
108-
this.notBefore = notBefore;
109-
this.notAfter = notAfter;
110-
}
111-
112-
/**
113-
* Create the object, decoding the values from the passed DER stream.
114-
*
115-
* @param in the DerInputStream to read the CertificateValidity from.
116-
* @exception IOException on decoding errors.
117-
*/
118-
public CertificateValidity(DerInputStream in) throws IOException {
119-
DerValue derVal = in.getDerValue();
120-
construct(derVal);
121-
}
122-
123114
/**
124115
* Return the validity period as user readable string.
125116
*/
126117
public String toString() {
127-
if (notBefore == null || notAfter == null)
128-
return "";
129118
return "Validity: [From: " + notBefore +
130119
",\n To: " + notAfter + ']';
131120
}
@@ -139,12 +128,6 @@ public String toString() {
139128
@Override
140129
public void encode(DerOutputStream out) throws IOException {
141130

142-
// in cases where default constructor is used check for
143-
// null values
144-
if (notBefore == null || notAfter == null) {
145-
throw new IOException("CertificateValidity:" +
146-
" null values to encode.\n");
147-
}
148131
DerOutputStream pair = new DerOutputStream();
149132

150133
if (notBefore.getTime() < YR_2050) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
/* @test
25+
* @bug 8296900
26+
* @summary CertificateValidity fields are not be optional
27+
* @library /test/lib
28+
* @modules java.base/sun.security.x509
29+
*/
30+
31+
import jdk.test.lib.Utils;
32+
import sun.security.x509.CertificateValidity;
33+
34+
import java.util.Date;
35+
36+
public class NullName {
37+
38+
public static void main(String[] argv) throws Exception {
39+
Date now = new Date();
40+
Utils.runAndCheckException(
41+
() -> new CertificateValidity(null, null),
42+
NullPointerException.class);
43+
Utils.runAndCheckException(
44+
() -> new CertificateValidity(now, null),
45+
NullPointerException.class);
46+
Utils.runAndCheckException(
47+
() -> new CertificateValidity(null, now),
48+
NullPointerException.class);
49+
new CertificateValidity(now, now);
50+
}
51+
}

0 commit comments

Comments
 (0)